package com.example.hello;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.FileProvider;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class LoginActivity extends AppCompatActivity {

    private static final int RESULT_LOAD_IMAGE = 1;
    private static final int TAKE_PHOTO = 1;
    private static final int CHOOSE_PHOTO = 2;
    private Button btn;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private ImageView image;
    private Uri imageUri;
    private int temp = 1;
    private static final int CAMERA_REQUEST = 1888;
    private TextView response;

    private boolean hasPermission() {
        return ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
    }

    private boolean hasPermission1() {
        return ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        btn = findViewById(R.id.btn2);
        btn1 = findViewById(R.id.btn_jump);
        btn2 = findViewById(R.id.takepho);
        image = findViewById(R.id.imgView);
        btn3=findViewById(R.id.button);
        response=findViewById(R.id.response);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //获取百度
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                image.setImageBitmap(null);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            URL url=new URL("http://www.baidu.com");
                            HttpURLConnection connection=(HttpURLConnection)url.openConnection();
                            connection.setRequestMethod("GET");
                            InputStream inputStream=connection.getInputStream();
                            BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
                            StringBuilder stringBuilder=new StringBuilder();
                            String Line;
                            while ((Line=reader.readLine())!=null){
                                stringBuilder.append(Line);
                            }
                            show(stringBuilder);
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
        //系统图片
        btn1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //打开相册
                response.setText(null);
                if (hasPermission1()) {
                    Intent i = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(i, RESULT_LOAD_IMAGE);
                    temp = 2;
                } else {
                    ActivityCompat.requestPermissions(LoginActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1000);
                }
            }
        });

        //相机
        btn2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                response.setText(null);
                takePhoto();

            }
        });
    }

    private void show(final StringBuilder stringBuilder) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                response.setText(stringBuilder);
            }
        });
    }

    private void takePhoto() {
        //创建File对象
        File outputImage = new File(getExternalCacheDir(), "output_image.jpg");
        try {
            if (outputImage.exists()) {
                outputImage.delete();
            }
            outputImage.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (Build.VERSION.SDK_INT >= 24) {
            imageUri = FileProvider.getUriForFile(LoginActivity.this,
                    "com.example.cameraalbumtest.fileprovider", outputImage);
        } else {
            imageUri = Uri.fromFile(outputImage);
        }
        //启动相机程序
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        startActivityForResult(intent, TAKE_PHOTO);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (temp == 2) {
            //获取图片
            Uri uri = data.getData();
            ContentResolver content = this.getContentResolver();
            Bitmap bitmap = null;
            try {
                bitmap = BitmapFactory.decodeStream(content.openInputStream(uri));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            image.setImageBitmap(bitmap);
            temp = 1;
        } else {
            if (resultCode == RESULT_OK) {
                try {
                    //将拍摄的照片显示出来
                    Bitmap bitmap1 = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
                    image.setImageBitmap(bitmap1);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}



